package com.societyGames.flashForms.basicSkin
{	
	import com.societyGames.flashForms.CenterScaleContainer;
	import com.societyGames.flashForms.DynamicTextField;
	import com.societyGames.flashForms.ExpanderContainer;
	import com.societyGames.flashForms.ExpanderHeaderContainer;
	import com.societyGames.flashForms.FourStateButton;
	import com.societyGames.flashForms.FrameContainer;
	import com.societyGames.flashForms.HorizontalSlidingDoorContainer;
	import com.societyGames.flashForms.HorizontalSplitContainer;
	import com.societyGames.flashForms.ItemsContainer;
	import com.societyGames.flashForms.ListItem;
	import com.societyGames.flashForms.ScrollContainer;
	import com.societyGames.flashForms.TabItem;
	import com.societyGames.flashForms.TabItemsContainer;
	import com.societyGames.flashForms.TextButton;
	import com.societyGames.flashForms.TreeViewNode;
	import com.societyGames.flashForms.VerticalScrollContainer;
	import com.societyGames.flashForms.VerticalSplitContainer;
	import com.societyGames.flashForms.Window;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.text.AntiAliasType;
	import flash.text.GridFitType;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.TextLineMetrics;

	public class BasicControls
	{	
		public static function createExpanderContainer():ExpanderContainer
		{
			var expanderContainer:ExpanderContainer = new ExpanderContainer(new BasicAssets.ExpanderCollapsedSpriteClass(),
				new BasicAssets.ExpanderExpandedSpriteClass(),
				new BasicAssets.ExpanderCollapsedOverSpriteClass(),
				new BasicAssets.ExpanderExpandedOverSpriteClass());
			return expanderContainer;
		}
		
		public static function createExpanderHeaderContainer():ExpanderHeaderContainer
		{
			var expanderHeaderContainer:ExpanderHeaderContainer = new ExpanderHeaderContainer(new BasicAssets.ExpanderCollapsedSpriteClass(),
				new BasicAssets.ExpanderExpandedSpriteClass(),
				new BasicAssets.ExpanderCollapsedOverSpriteClass(),
				new BasicAssets.ExpanderExpandedOverSpriteClass());
			return expanderHeaderContainer;
		}
		
		public static const TEXT_BUTTON_HEIGHT:int = 28;
		
		public static function createTextButton(clickHandler:Function /*(Event):void*/):TextButton
		{	
			//Create a generic textField.
			var textField:TextField = createTextField();
			//Center it.
			var textFormat:TextFormat = textField.defaultTextFormat;
			textFormat.align = TextFormatAlign.CENTER;
			textField.defaultTextFormat = textFormat;
			//Create the button with the textField.
			var textButton:TextButton = new TextButton(new BasicAssets.ButtonClass(), new BasicAssets.SelectedButtonClass(), new BasicAssets.DownButtonClass(), textField);
			textButton.buttonClickEventObject.addEventListener(clickHandler);
			textButton.height = TEXT_BUTTON_HEIGHT;
			return textButton;
		}

		public static function createTextField():TextField
		{
			var textField:TextField = new DynamicTextField();
			textField.antiAliasType = AntiAliasType.ADVANCED;
			textField.gridFitType = GridFitType.PIXEL;
			textField.useRichTextClipboard = true;
			
			//Arial
			var newFormat:TextFormat = new TextFormat();
			newFormat.font = "Arial";
			newFormat.size = 12;
			textField.defaultTextFormat = newFormat;
			
			return textField;
		}
		
		public static function createSingleLineTextField(text:String):TextField
		{	
			var textField:TextField = createTextField();
			var textFormat:TextFormat = textField.defaultTextFormat;
			textFormat.align = TextFormatAlign.CENTER;
			textField.defaultTextFormat = textFormat;
			
			textField.text = text;
			var metrics:TextLineMetrics = textField.getLineMetrics(0);
			textField.width = metrics.width + 4;
			textField.height = metrics.height + 4;
			return textField;
		}
		
		public static function createHtmlTextField():TextField
		{
			var textField:TextField = createTextField();
			textField.condenseWhite = true;
			textField.styleSheet = createHtmlStyleSheet();
			return textField;
		}
		
		public static function createHtmlStyleSheet():StyleSheet
		{
			var style:StyleSheet = new StyleSheet();
			//style.parseCSS("body{font-family:Arial}a:link{color: #0000FF;text-decoration: underline;}a:hover{color:#444444;text-decoration:underline;}");
			style.parseCSS("a:link{color: #0000FF;text-decoration: underline;}a:hover{color:#444444;text-decoration:underline;}");
			return style;
		}
		
		public static function createDefaultFontTextField():TextField
		{
			var textField:TextField = createTextField();
			/*var textFormat:TextFormat = new TextFormat();
			textFormat.font = "Arial";
			textFormat.size = 12;
			textField.setTextFormat(textFormat);*/
			return textField;
		}
		
		public static function createVerticalTextField():TextField
		{
			var textField:TextField = createDefaultFontTextField();
			textField.autoSize = TextFieldAutoSize.LEFT;
			textField.multiline = true;
			return textField;
		}
		
		public static function createScrollContainer():ScrollContainer
		{
			var horizontalTrack:Sprite = new BasicAssets.HorizontalScrollBarTrackClass();
			var horizontalThumb:Sprite = createHorizontalThumb();
			
			var verticalTrack:Sprite = new BasicAssets.VerticalScrollBarTrackClass();
			var verticalThumb:Sprite = createVerticalThumb();
			
			var scrollContainer:ScrollContainer = new ScrollContainer(
				horizontalTrack,
				horizontalThumb,
				verticalTrack,
				verticalThumb
			);
			return scrollContainer;
		}
		
		private static function createHorizontalThumb():Sprite
		{
			var horizontalThumb:Sprite = new BasicAssets.HorizontalScrollBarThumbClass();
			//The thumb is composed of two pieces: the actual thumb and the grip.
			//Create a self centering and disappearing grip.
			var horizontalGrip:DisplayObject = new BasicAssets.HorizontalScrollBarGripClass();
			var centerScaleContainer:CenterScaleContainer = new CenterScaleContainer();
			centerScaleContainer.child = horizontalGrip;
			//Layer the grip on the thumb.
			var itemsContainer:ItemsContainer = new ItemsContainer();
			//Make sure the grip is the same height as the thumb.
			itemsContainer.height = horizontalThumb.height;
			itemsContainer.addItem(horizontalThumb);
			itemsContainer.addItem(centerScaleContainer);
			return itemsContainer;
		}

		public static function createVerticalScrollContainer():VerticalScrollContainer
		{
			var verticalTrack:Sprite = new BasicAssets.VerticalScrollBarTrackClass();
			var verticalThumb:Sprite = createVerticalThumb();
			//Create the actual scroll container
			var verticalScrollContainer:VerticalScrollContainer = new VerticalScrollContainer(
				verticalTrack,
				verticalThumb
			);
			return verticalScrollContainer;
		}
		
		private static function createVerticalThumb():Sprite
		{
			var verticalThumb:Sprite = new BasicAssets.VerticalScrollBarThumbClass();
			//The thumb is composed of two pieces: the actual thumb and the grip.
			//Create a self centering and disappearing grip.
			var verticalGrip:DisplayObject = new BasicAssets.VerticalScrollBarGripClass();
			var centerScaleContainer:CenterScaleContainer = new CenterScaleContainer();
			centerScaleContainer.child = verticalGrip;
			//Layer the grip on the thumb.
			var itemsContainer:ItemsContainer = new ItemsContainer();
			//Make sure the grip is the same width as the thumb.
			itemsContainer.width = verticalThumb.width;
			itemsContainer.addItem(verticalThumb);
			itemsContainer.addItem(centerScaleContainer);
			return itemsContainer;
		}
		
		public static function createTabItem(headerName:String, content:Sprite):TabItem
		{
			var textField:TextField = createDefaultFontTextField();
			textField.autoSize = TextFieldAutoSize.LEFT;			
			textField.text = headerName;
			textField.mouseEnabled = false;
			
			var tabHeader:Sprite = Sprite(new BasicAssets.TabHeaderClass());
			tabHeader.width = tabHeader.scale9Grid.left + (tabHeader.width - tabHeader.scale9Grid.right) + textField.width;
			tabHeader.height = tabHeader.scale9Grid.top + (tabHeader.height - tabHeader.scale9Grid.bottom) + textField.height;
			
			textField.x = (tabHeader.scale9Grid.left - textField.getLineMetrics(0).x) / tabHeader.scaleX;
			textField.y = tabHeader.scale9Grid.top / tabHeader.scaleY;
			textField.scaleX = 1 / tabHeader.scaleX;
			textField.scaleY = 1 / tabHeader.scaleY;			
			tabHeader.addChild(textField);
			
			return new TabItem(tabHeader, content);
		}
		
		public static function createWindow(name:String = null):Window
		{
			var windowSprite:Sprite = new BasicAssets.WindowSpriteClass();
			var window:Window = new Window(windowSprite, 8, 8, 8, 8, 18);
			if (name != null)
			{
				var textField:TextField = createDefaultFontTextField();
				textField.selectable = false;
				textField.text = name;
				window.header = textField;
			}
			return window;
		}
		
		public static function createDebugCircle(radius:Number):Sprite
		{
			var sprite:Sprite = new Sprite();
			sprite.graphics.beginFill(0xFF0000);
			sprite.graphics.drawCircle(radius, radius, radius);
			return sprite;
		}
		
		public static function createVerticalSplitContainer():VerticalSplitContainer
		{
			var splitter:Sprite = new BasicAssets.SplitterClass();
			var verticalSplitContainer:VerticalSplitContainer = new VerticalSplitContainer(splitter);
			return verticalSplitContainer;
		}
		
		public static function createHorizontalSplitContainer():HorizontalSplitContainer
		{
			var splitter:Sprite = new BasicAssets.SplitterClass();
			var horizontalSplitContainer:HorizontalSplitContainer = new HorizontalSplitContainer(splitter);
			return horizontalSplitContainer;
		}
		
		public static function createHorizontalSlidingDoorContainer():HorizontalSlidingDoorContainer
		{
			var leftSplitter:Sprite = new BasicAssets.SplitterClass();
			var rightSplitter:Sprite = new BasicAssets.SplitterClass();
			var horizontalSlidingDoorContainer:HorizontalSlidingDoorContainer = new HorizontalSlidingDoorContainer(leftSplitter, rightSplitter);
			return horizontalSlidingDoorContainer;
		}

		public static function createListItem(text:String, textColor:uint = 0, isBold:Boolean = false):ListItem
		{	
			var unselected:FrameContainer = createListItemFrameContainer(text, new Sprite(), textColor, isBold);
			var over:FrameContainer = createListItemFrameContainer(text, new BasicAssets.ListItemOverClass(), textColor, isBold);
			var selected:FrameContainer = createListItemFrameContainer(text, new BasicAssets.ListItemSelectedClass(), textColor, isBold);
			var listItem:ListItem = new ListItem(unselected, selected, over);
			return listItem;
		}

		private static function createListItemFrameContainer(text:String, backGround:Sprite, textColor:uint = 0, isBold:Boolean = false):FrameContainer
		{
			var textField:TextField = createDefaultFontTextField();
			textField.antiAliasType = AntiAliasType.ADVANCED;
			textField.textColor = textColor;
			if (isBold)
			{
				textField.htmlText = "<b>" + text + "</b>";
			}
			else
			{
				textField.htmlText = text;
			}
			textField.height = 19;
			textField.mouseEnabled = false;
			
			var frameContainer:FrameContainer = new FrameContainer(backGround, 1, 1, 0, 0);
			frameContainer.height = 20;
			frameContainer.child = textField;
			frameContainer.buttonMode = true;
			frameContainer.useHandCursor = true;
			
			return frameContainer;
		}
		
		public static function createTabItemsContainer():TabItemsContainer
		{
			var tabLine:Bitmap = new BasicAssets.TabLineClass();
			var tabItemsContainer:TabItemsContainer = new TabItemsContainer(new BasicAssets.TabLineClass());
			return tabItemsContainer;
		}
		
		public static function createToolTipFrameContainer():FrameContainer
		{
			var frameContainer:FrameContainer = new FrameContainer(new BasicAssets.ToolTipClass(), 3, 3, 3, 3);
			return frameContainer;
		}
	}
}